/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.smscbresponse;

import com.elcom.emocbc.mongodb.CellBroadcastResult;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.io.IOUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author GiangLT
 */
public class GetMessageStatusResponseParser {

    public static List<CellBroadcastResult> parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            GetMessageStatusResponseHandler defaultHandler = new GetMessageStatusResponseHandler();
            saxParser.parse(inputStream, defaultHandler);
            return defaultHandler.getCellBroadcastResults();
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static List<CellBroadcastResult> parse(String xml) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        GetMessageStatusResponseHandler defaultHandler = new GetMessageStatusResponseHandler();
        InputSource inputSource = new InputSource(new StringReader(xml));
        saxParser.parse(inputSource, defaultHandler);
        return defaultHandler.getCellBroadcastResults();
    }
    ////<editor-fold defaultstate="collapsed" desc="DOM parse">
//    public static List<CellBroadcastResult> parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
    //        DocumentBuilder dBuilder = null;
    //
    //        try {
    //            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    //            dBuilder = dbFactory.newDocumentBuilder();
    //
    //            Document doc = dBuilder.parse(inputStream);
    //            doc.getDocumentElement().normalize();
    //
    //            String broadcastMessageId = doc.getElementsByTagName("messageId").item(0).getTextContent();
    //
    //            List<CellBroadcastResult> cellBroadcastResults = new ArrayList<CellBroadcastResult>();
    //            NodeList cbcMessageStatusPerBscNodes = doc.getElementsByTagName("cbcMessageStatusPerBsc");
    //            for (int i = 0; i < cbcMessageStatusPerBscNodes.getLength(); i++) {
    //                Element node = (Element) cbcMessageStatusPerBscNodes.item(i);
    //                String bscId = node.getElementsByTagName("bscId").item(0).getTextContent();
    //
    //                NodeList broadcastInformationNodes = node.getElementsByTagName("broadcastInformation");
    //                if (broadcastInformationNodes != null && broadcastInformationNodes.getLength() > 0) {
    //                    for (int j = 0; j < broadcastInformationNodes.getLength(); j++) {
    //                        CellBroadcastResult cellBroadcastResult = new CellBroadcastResult();
    //                        cellBroadcastResult.setBroadcastMessageId(broadcastMessageId);
    //                        cellBroadcastResult.setBscId(bscId);
    //
    //                        Element broadcastInformationNode = (Element) broadcastInformationNodes.item(i);
    //                        String cellId = broadcastInformationNode.getElementsByTagName("cellId").item(0).getTextContent();
    //                        cellBroadcastResult.setCellId(cellId);
    //
    //                        NodeList numberOfBroadcastCompleteNodes = broadcastInformationNode.getElementsByTagName("numberOfBroadcastComplete");
    //                        if (numberOfBroadcastCompleteNodes != null && numberOfBroadcastCompleteNodes.getLength() > 0) {
    //                            cellBroadcastResult.setBroadcastFailed(false);
    //                            String numberOfBroadcastComplete = numberOfBroadcastCompleteNodes.item(0).getTextContent();
    //                            cellBroadcastResult.setNumberOfBrodcastComplete(Integer.valueOf(numberOfBroadcastComplete));
    //                        }
    //
    //                        NodeList causeNodes = broadcastInformationNode.getElementsByTagName("cause");
    //                        if (causeNodes != null && causeNodes.getLength() > 0) {
    //                            cellBroadcastResult.setBroadcastFailed(true);
    //                            cellBroadcastResult.setFailureCause(causeNodes.item(0).getTextContent());
    //                        }
    //                        cellBroadcastResults.add(cellBroadcastResult);
    //                    }
    //                }
    //            }
    //            return cellBroadcastResults;
    //        } finally {
    //            if (dBuilder != null) {
    //                dBuilder.reset();
    //            }
    //            IOUtils.closeQuietly(inputStream);
    //        }
    //    }
    //</editor-fold>
//    <getmessagestatusResp>
//  <cbcResponse>
//    <request>getmessagestatus</request>
//    <returnValue type="list">
//      <cbcMessageStatusResult>
//        <messageId>11</messageId>
//        <statusPerBsc>
//          <cbcMessageStatusPerBsc>
//            <bscId>4</bscId>
//            <failureList />
//            <numberOfBroadcastCompleteList>
//              <broadcastInformation>
//                <cellId>1.1.0.0</cellId>
//                <numberOfBrodcastComplete>
//                3</numberOfBrodcastComplete>
//              </broadcastInformation>
//              <broadcastInformation>
//                <cellId>10.9.0.0</cellId>
//                <numberOfBrodcastComplete>
//                3</numberOfBrodcastComplete>
//              </broadcastInformation>
//              <broadcastInformation>
//                <cellId>6.5.0.0</cellId>
//                <numberOfBrodcastComplete>
//                3</numberOfBrodcastComplete>
//              </broadcastInformation>
//              <broadcastInformation>
//                <cellId>8.7.0.0</cellId>
//                <numberOfBrodcastComplete>
//                3</numberOfBrodcastComplete>
//              </broadcastInformation>
//            </numberOfBroadcastCompleteList>
//          </cbcMessageStatusPerBsc>
//          <cbcMessageStatusPerBsc>
//            <bscId>5</bscId>
//            <faliureList />
//            <numberOfBroadcastCompleteList>
//              <broadcastInformation>
//                <cellId>4.4.1100.1</cellId>
//                <numberOfBrodcastComplete>
//                0</numberOfBrodcastComplete>
//              </broadcastInformation>
//              <broadcastInformation>
//                <cellId>1.1.1100.1</cellId>
//                <numberOfBrodcastComplete>
//                0</numberOfBrodcastComplete>
//              </broadcastInformation>
//              <broadcastInformation>
//                <cellId>2.2.1100.1</cellId>
//                <numberOfBrodcastComplete>
//                0</numberOfBrodcastComplete>
//              </broadcastInformation>
//              <broadcastInformation>
//                <cellId>3.3.1100.1</cellId>
//                <numberOfBrodcastComplete>
//                0</numberOfBrodcastComplete>
//              </broadcastInformation>
//              <broadcastInformation>
//                <cellId>0.0.1100.1</cellId>
//                <numberOfBrodcastComplete>
//                0</numberOfBrodcastComplete>
//              </broadcastInformation>
//            </numberOfBroadcastCompleteList>
//          </cbcMessageStatusPerBsc>
//        </statusPerBsc>
//      </cbcMessageStatusResult>
//    </returnValue>
//    <status>SUCCESS</status>
//  </cbcResponse>
//  <cbcResponse>
//    <request>getmessagestatus</request>
//    <returnValue type="list">
//      <cbcMessageStatusResult>
//        <messageId>1</messageId>
//        <statusPerBsc>
//          <cbcMessageStatusPerBsc>
//            <bscId>1</bscId>
//            <reject>
//              <cause>VALID CBS MESSAGE NOT IDENTIFIED</cause>
//            </reject>
//          </cbcMessageStatusPerBsc>
//          <cbcMessageStatusPerBsc>
//            <bscId>2</bscId>
//            <reject>
//              <cause>VALID CBS MESSAGE NOT IDENTIFIED</cause>
//            </reject>
//          </cbcMessageStatusPerBsc>
//          <cbcMessageStatusPerBsc>
//            <bscId>3</bscId>
//            <reject>
//              <cause>VALID CBS MESSAGE NOT IDENTIFIED</cause>
//            </reject>
//          </cbcMessageStatusPerBsc>
//        </statusPerBsc>
//      </cbcMessageStatusResult>
//    </returnValue>
//    <status>SUCCESS</status>
//  </cbcResponse>
//</getmessagestatusResp>
}
